<!-- sku规格选择弹框组件 -->
<template>
  <view class="popup-container">
    <view class="popup-bg" @click="$emit('closePopup')"></view>
    <view class="popup-content">
      <image
        class="close-icon"
        @click="$emit('closePopup')"
        :src="asyncImgs.alipay.close_grey_icon"
        mode=""
      />
      <view class="content-head">
        <image :src="skuInfo.goods_pic" mode="" />
        <view class="good-info">
          <view class="name">{{ skuInfo.name }}</view>
          <view class="atter" v-if="chooseSkuText"
            >{{$t('goods.choosedSku')}}：{{ chooseSkuText }}</view
          >
          <view class="price">
            <text>￥</text>
            <text>{{ skuInfo.price }}</text>
            <text>￥{{ skuInfo.original_price }}</text>
          </view>
        </view>
      </view>
      <view class="content-body">
        <view class="atter-list-box">
          <view class="item-info" v-for="(item, index) in skuList" :key="index">
            <view class="attr-name">{{ item.name + ":" }}</view>
            <view class="attr-list">
              <view
                class="attr-info"
                :class="{ active: item.active == attr }"
                v-for="(attr, i) in item.list"
                :key="i"
                @click="handleAtterItem(index, attr, i)"
                >{{ attr }}</view
              >
            </view>
          </view>
        </view>
        <view class="good-num">
          <view class="label">{{$t('goods.number')}}</view>
          <view class="action-box">
            <view class="reduce" @click="reduceOne">
              <view></view>
            </view>
            <view class="num">{{ num }}</view>
            <view class="add" @click="addOne">
              <image
                :src="asyncImgs.alipay.white_add_icon"
                mode=""
              />
            </view>
          </view>
        </view>
      </view>
      <view class="content-foot">
        <view class="foot-btn" @click="chooseEnd">{{$t('alipay.choose')}}</view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    skuInfo: {
      type: Object,
      defalut: {},
    },
  },
  data() {
    return {
      num: 1,
      skuList: [],
    };
  },
  watch: {},
  mounted() {
    if (!this.skuInfo.sku) {
      this.skuInfo.attr.forEach((d) => {
        d["active"] = d.list[0];
      });
    } else {
      this.skuInfo.attr.forEach((d) => {
        for (let i = 0; i < this.skuInfo.sku.length; i++) {
          if (this.skuInfo.sku[i].name == d.name) {
            d.active = this.skuInfo.sku[i].value;
          }
        }
      });
      this.num = this.skuInfo.num;
    }

    this.skuList = this.skuInfo.attr;
  },

  computed: {
    chooseSkuText() {
      let str = "";
      this.skuList.forEach((d) => {
        str = str + d.name + ": " + d.active + ";";
      });
      return str;
    },
  },
  methods: {
    // 点击选择sku属性
    handleAtterItem(attrI, attr, i) {
      this.skuList.forEach((d, index) => {
        if (index == attrI) {
          d.active = d.list[i];
        }
      });
    },
    // 添加一个
    addOne() {
      this.num++;
    },
    // 删掉一个
    reduceOne() {
      if (this.num <= 0) return;
      this.num--;
    },
    // 点击选好了
    chooseEnd() {
      let arr = [];
      this.skuList.forEach((d) => {
        let obj = { name: d.name, value: d.active };
        arr.push(obj);
      });
      let data = {
        ...this.skuInfo,
        sku: arr,
      };
      data.num = this.num;
      this.$emit("chooseEnd", { data });
    },
  },
};
</script>

<style lang="less" scoped>
.popup-container {
  width: 100%;
  height: 100vh;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1002;
  .popup-bg {
    width: 100%;
    height: 100vh;
    background: #000000;
    opacity: 0.5;
  }
  .popup-content {
    width: 100%;
    position: absolute;
    left: 0;
    bottom: 0;
    z-index: 151;
    background: #ffffff;
    border-radius: 4vw 4vw 0 0;
    padding: 4.27vw;
    overflow: auto;
    max-height: 148.53vw;

    .close-icon {
      width: 4.73vw;
      height: 4.73vw;
      position: absolute;
      right: 2.87vw;
      top: 3vw;
      z-index: 11;
      background-size: 100% 100%;
    }

    .content-head {
      display: flex;
      align-items: flex-start;
      image {
        width: 25.33vw;
        height: 25.33vw;
        background-color: #ffffff;
        background-size: 100% 100%;
        border: 0.13vw solid rgba(112, 112, 112, 0.10196078431372549);
        border-radius: 1.33vw;
        margin-right: 2.67vw;
      }
      .good-info {
        flex: 1;
        .name {
          font-size: 4.8vw;
          font-weight: 600;
          color: #191919;
        }
        .atter {
          font-size: 2.93vw;
          font-weight: 400;
          line-height: 4.27vw;
          color: #666666;
          margin-top: 0.4vw;
        }
        .price {
          display: flex;
          align-items: flex-end;
          font-size: 6.4vw;
          font-family: PingFang SC;
          font-weight: 600;
          line-height: 4.27vw;
          color: #fd4b32;
          margin-top: 6.8vw;
          text:first-child {
            font-size: 4.8vw;
          }
          text:nth-child(2) {
            line-height: 5.2vw;
          }
          text:last-child {
            font-size: 2.93vw;
            font-weight: 600;
            color: #999999;
            text-decoration: line-through;
            margin-left: 1.33vw;
          }
        }
      }
    }

    .content-body {
      font-size: 3.73vw;
      font-weight: 400;
      line-height: 4.27vw;
      color: #191919;
      min-height: 35vh;
      margin-top: 9vw;
      .atter-list-box {
        margin-top: 3.47vw;
        .item-info {
          .attr-name {
            font-size: 3.73vw;
            font-weight: 400;
            line-height: 4.27vw;
            color: #191919;
            margin-bottom: 3.47vw;
          }
          .attr-list {
            display: flex;
            flex-wrap: wrap;
            .attr-info {
              width: 27.73vw;
              height: 9.33vw;
              background: #f5f5f5;
              border-radius: 1.6vw;
              display: flex;
              justify-content: center;
              align-items: center;
              font-size: 3.47vw;
              color: #666666;
              margin-right: 2.67vw;
              margin-bottom: 3.47vw;
            }
            .active {
              background: #ccf0ff;
              color: #1fb6fd;
              font-weight: 600;
            }
          }
        }
        .item-info:last-child {
          margin-right: 0;
        }
      }
      .good-num {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .action-box {
          display: flex;
          align-items: center;
          .reduce {
            width: 5.87vw;
            height: 5.87vw;
            background: #ffffff;
            border: 0.27vw solid #c8c8c8;
            border-radius: 8vw;
            display: flex;
            justify-content: center;
            align-items: center;
            view {
              width: 3.2vw;
              height: 0.53vw;
              background: #c8c8c8;
            }
          }
          .num {
            margin: 0 2.93vw;
          }
          .add {
            width: 5.87vw;
            height: 5.87vw;
            background: #1fb6fd;
            border-radius: 8vw;
            display: flex;
            justify-content: center;
            align-items: center;
            image {
              width: 5vw;
              height: 5vw;
              background-size: 100% 100%;
              display: block;
            }
          }
        }
      }
    }

    .content-foot {
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      padding-top: 6.53vw;
      .foot-btn {
        width: 91.47vw;
        height: 10.93vw;
        background: #1fb6fd;
        border-radius: 8vw;
        text-align: center;
        line-height: 10.93vw;
        font-size: 4.27vw;
        font-weight: 600;
        color: #ffffff;
      }
    }
  }
}
</style>
